package westh.ilib.struts.action.tagRetrive;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.ilib.bean.tagBean.TagSys;

/**
 * @author zhangjian
 * 
 */

public class GetRscsByTagAction extends Action {
	private final static Log log = LogFactory.getLog(GetRscsByTagAction.class);
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
		westh.log.YyLog.addFootprint(request, "动作-GetRscsByTagAction");

		JSONObject resultObject = new JSONObject();

		//目标标签
		String tagName = request.getParameter("tagName");
		//查询类别
		String isFuzzy = request.getParameter("isFuzzy");
		//个性化搜索
		String isPersonalized = request.getParameter("isPersonalized");
		//获取资源类别
		String[] resType = request.getParameterValues("resType");	
		String resTypeId="";
		JSONObject resultRes = new JSONObject();
		if (resType != null && resType.length != 0) {
			for(int i=0;i<resType.length;i++){
				resTypeId = resType[i];
				log.debug("resTypeId="+resTypeId);
				resultRes.put(resTypeId, "1");
			}
		}
		JSONArray resourceArry = new JSONArray();
		resourceArry.add(resultRes);
		//获取资源域
		String[] fieldType = request.getParameterValues("fieldType");	
		String fieldTypeId="";
		JSONObject resultField = new JSONObject();
		if (fieldType != null && fieldType.length != 0) {
			for(int i=0;i<fieldType.length;i++){
				fieldTypeId = fieldType[i];
				log.debug("fieldTypeId="+fieldTypeId);
				resultField.put(fieldTypeId, "1");
			}
			
			if(fieldType.length<5){
				resultField.put("4", "0");
				resultField.put("5", "0");
			}
		}
		JSONArray fieldArry = new JSONArray();
		fieldArry.add(resultField);
		
		
		resultObject.put("tagName", tagName);
		resultObject.put("isFuzzy", isFuzzy);
		resultObject.put("isPersonal", isPersonalized);
		resultObject.put("resourceTypeList", resourceArry);
		resultObject.put("resourceFieldList", fieldArry);
		
		String inputStr = resultObject.toString();
		log.debug("inputStr="+inputStr);
		
		//jSon-String
		String outString = "";
		/**************获取jSon信息 start***************************/		
		try {
			MuleClient client = new MuleClient();
			//调用WebService
			UMOMessage umo = client.send("axis:http://166.111.134.22:5181/Test/yyiLibTest?method=getRscsByTag",inputStr,null);
			outString = (String)umo.getPayload();

		
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		/**************获取jSon信息 end***************************/
		
		String retCode=""; //返回代码
		String retMsg="";//返回结果信息
		String returnStr="";//跳转信息
		/****************解析输出jSon串   start*********************/
		JSONObject jsonOut= JSONObject.fromObject(outString);
		retCode = jsonOut.getString("retCode");
		if (retCode.equals("1100")) {

			String result = jsonOut.getString("result");
			JSONArray resultArry = JSONArray.fromObject(result);
			log.info("JSONArray="+resultArry.size());
			//解析数组
			TagSys tagsys = new TagSys();
			List list = new ArrayList();
			for(int i=0;i<resultArry.size();i++){
				tagsys = new TagSys();
				JSONObject jsonOut2 = JSONObject.fromObject(resultArry.get(i));
				tagsys.setRscName(jsonOut2.getString("rscName"));
				tagsys.setRscType(jsonOut2.getString("rscType"));
				tagsys.setRscPubTime(jsonOut2.getString("rscPubTime"));
				tagsys.setRscAuthor(jsonOut2.getString("rscAuthor"));
				tagsys.setRscSource(jsonOut2.getString("rscSource"));
				list.add(tagsys);

			}
			/****************解析输出jSon串   end*********************/
			request.setAttribute("list", list);
			returnStr = "succ";
		}
		
		if (retCode.equals("1101")) {
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			returnStr = "fail";
		}
		if (retCode.equals("1102")) {
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			returnStr = "fail";
		}
		return mapping.findForward(returnStr);
		
	}

}
